package com.bupt.liutong.admin.core.bo;

import static com.bupt.liutong.admin.core.base.Constants.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.bupt.liutong.admin.core.base.BaseBo;
import com.bupt.liutong.admin.core.base.BaseDto;
import com.bupt.liutong.admin.core.dao.PopWindowDao;
import com.bupt.liutong.admin.core.dto.PopWindow;
import com.bupt.liutong.admin.core.dto.PopWindowConfig;
import com.bupt.liutong.admin.core.dto.PopWindowParser;
import com.bupt.liutong.admin.core.form.PopWindowForm;
import com.bupt.liutong.util.BeanUtils;
import com.bupt.liutong.util.FileUtils;
import com.bupt.liutong.util.StringUtils;

public class PopWindowBo extends BaseBo {
	
	private PopWindowDao popWindowDao;
	private final PopWindowConfig popWindowConfig = PopWindowParser.loadConfig(FileUtils.getProjectPath()+"admin/core/pop_window/pop_window.xml");
	
	public List<Map<String, Object>> getListByPage(PopWindowForm popWindowForm, HttpServletRequest request) {
		// 1、取配置：从pop_window.xml中，取弹出窗口的配置
		if (popWindowForm.getSearchFlag().equals(POP_WINDOW_OPEN_FLAG)) {
			this.loadConfig(popWindowForm, request);
		}
		
		// 2、取参数：把查询的参数值从request里取出放到Map里；再从Map转换到Dto里
		Map<String, String> searchPropMap = new HashMap<String, String>();
		for (String searchProp : popWindowForm.getSearchPropArr()) {
			String searchPropValue = request.getParameter(searchProp);
			if (!StringUtils.isEmpty(searchPropValue)) {
				searchPropMap.put(searchProp, searchPropValue);
				request.setAttribute(searchProp, searchPropValue);
			}
		}
		BaseDto baseDto = (BaseDto) BeanUtils.newInstance(popWindowForm.getDto());
		BeanUtils.copyProperties(baseDto, searchPropMap);
		
		// 3、查总数
		sm.getPopWindowForm(popWindowForm, baseDto);
		
		// 4、拼“回写”值：组装查询结果列表中的radio的value，这个value值就是写回原页面的值，是由","分隔的字符串
		List<Map<String, Object>> resultList = popWindowDao.getListByPage(popWindowForm.getSqlMapId(), baseDto);
		for(Map<String, Object> map:resultList) {
			List<String> writeBackValueList = new ArrayList<String>();
			for(String writeBackProp:popWindowForm.getWriteBackPropArr()) {
				if (map.get(writeBackProp) == null)
					writeBackValueList.add("");
				else
					writeBackValueList.add(map.get(writeBackProp).toString());
			}
			map.put("writeBackValue", StringUtils.join(writeBackValueList, ","));
		}
		return resultList;
	}
	
	private void loadConfig(PopWindowForm popWindowForm, HttpServletRequest request) {
		// 取得弹出窗口的配置参数
		List<PopWindow> popWindowList = popWindowConfig.getPopWindowList();
		PopWindow popWindow = null;
		for (int i = 0; i < popWindowList.size(); i++) {
			popWindow = popWindowList.get(i);
			if (popWindow.getWindowName().equals(popWindowForm.getWindowName())) {
				BeanUtils.copyProperties(popWindowForm, popWindow);
				break;
			}
		}
		if (!popWindow.getWindowName().equals(popWindowForm.getWindowName()))
			throw new IllegalArgumentException();
	}

	public void setPopWindowDao(PopWindowDao popWindowDao) {
		this.popWindowDao = popWindowDao;
	}
}
